package cn.codesea.housework_user.ui.manager;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.entity.Activity;
import cn.codesea.housework_user.entity.Service;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.ActivityService;
import cn.codesea.housework_user.net.service.SerService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SerEditActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.submit_tv)
    TextView submit_tv;

    @BindView(R.id.serName_tv)
    TextView serName_tv;
    @BindView(R.id.serDesc_et)
    EditText serDesc_et;

    @BindView(R.id.serviceType_tv)
    TextView serviceType_tv;
    @BindView(R.id.serPrice_et)
    EditText serPrice_et;


    Service ser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ser_edit);
        ButterKnife.bind(this);
        init();
    }

    public void init(){
        initData();
        initView();
    }

    public void initData(){
        ser = (Service) getIntent().getSerializableExtra("ser");
    }

    public void initView(){
        initToolbar();
        if (ser != null){
            serName_tv.setText(ser.getName());
            serDesc_et.setText(ser.getDesc());
            serviceType_tv.setText(ser.getServicetype());
            serPrice_et.setText(ser.getPrice());
        }

    }

    public void initToolbar(){
        // 初始化 toolbar
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick(R.id.submit_tv)
    public void submit_tv(){

        ser.setDesc(serDesc_et.getText().toString());
        ser.setServicetype(serviceType_tv.getText().toString());
        ser.setPrice(serPrice_et.getText().toString());

        SerService service = ServiceGenerator.createService(SerService.class);
        Call<Result> call = service.updateService(ser);
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body(),"更新");
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                Toast.makeText(SerEditActivity.this,"网络错误!",Toast.LENGTH_SHORT).show();
            }
        });

    }

    /**
     * 结果处理
     */
    public void resultProcess(Result result,String msg){
        if (result.getState() == 200){
            Dialog dialog = new AlertDialog.Builder(SerEditActivity.this)
                    .setTitle("提示")
                    .setMessage(msg + "成功！")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startActivity(new Intent(SerEditActivity.this,ManagerMainActivity.class));
                        }
                    })
                    .create();
            dialog.setCancelable(false);
            dialog.show();
        }else {
            Dialog dialog = new AlertDialog.Builder(SerEditActivity.this)
                    .setTitle("提示")
                    .setMessage(msg + "失败！")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    })
                    .create();
            dialog.setCancelable(false);
            dialog.show();
        }
    }
}
